package com.ipay.wechat.util;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipaySystemOauthTokenRequest;
import com.alipay.api.request.AlipayUserInfoShareRequest;
import com.alipay.api.request.AlipayUserUserinfoShareRequest;
import com.alipay.api.response.AlipaySystemOauthTokenResponse;
import com.alipay.api.response.AlipayUserInfoShareResponse;
import com.alipay.api.response.AlipayUserUserinfoShareResponse;
import com.ipay.utils.DateUtils;
import com.ipay.utils.MapUtils;
import com.ipay.utils.StringUtil;
import com.ipay.wechat.pojo.UserWechatInfo;
import com.ipay.wechat.service.UserWechatInfoService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

public class ExecuteWeChat {
	@Autowired
	private UserWechatInfoService uwiService;

	public static synchronized String getOauthInfo(String code, String appID, String appSecret) {
		String oauth2Url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=" + appID + "&secret=" + appSecret
				+ "&code=" + code + "&grant_type=authorization_code"; // 由code值换取token
		String result = HttpUtils.requestHttp(oauth2Url, "get", "");
		return result;
	}

	public static synchronized String getOauthUserInfo(String access_token, String openid) {
		String userInfoUrl = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid
				+ "&lang=zh_CN";
		String userInfoResult = HttpUtils.requestHttp(userInfoUrl, "GET", "");
		return userInfoResult;
	}

	public static synchronized String getBaseUserInfo(String access_token, String openid) {
		String userInfoResult = HttpUtils.requestHttp("https://api.weixin.qq.com/cgi-bin/user/info?access_token="
				+ access_token + "&openid=" + openid + "&lang=zh_CN", "get", "");
		return userInfoResult;
	}

	public static synchronized String getWuLiuInfo(String de_code, String de_number) {
		String userInfoResult = HttpUtils.requestHttp("http://m.kuaidi100.com/query?type=" + de_code + "&postid="
				+ de_number + "&id=1&valicode=&temp=0.5313415713608265", "GET", "");
		return userInfoResult;
	}

	/*
	 * public static synchronized String getOauthInfo(String code){ String
	 * oauth2Url =
	 * "https://api.weixin.qq.com/sns/oauth2/access_token?appid="+MyData.appID+
	 * "&secret="+MyData.appsecret +
	 * "&code="+code+"&grant_type=authorization_code"; //由code值换取token String
	 * result = HttpUtils.requestHttp(oauth2Url, "get", ""); return result; }
	 */

	public static synchronized String getUserBase(String access_token, String openid) {
		String oauth2Url = "https://api.weixin.qq.com/sns/userinfo?access_token=" + access_token + "&openid=" + openid
				+ "&lang=zh_CN ";
		String result = HttpUtils.requestHttp(oauth2Url, "get", "");
		return result;
	}

	/**
	 *
	 * @param code
	 * @param appID
	 * @param key
	 * @param publicKey
	 * @return
	 */
	public static synchronized AlipayUserUserinfoShareResponse getAliOauthInfo(String code, String appID,String key,String publicKey) {
		String oauth2Url = "https://openapi.alipay.com/gateway.do"; // 由auth_code值换取OpenId
		/*String params = "app_id=" + appID + "&grant_type=authorization_code&code=" + code+
				"&method=alipay.system.oauth.token&timestamp="+DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN)
				+"&sign_type=RSA";*/
		//String userid = "";
		/*Map<String, String> paraMap = new HashMap<String, String>();
		paraMap.put("app_id", appID);
		paraMap.put("code", code);
		paraMap.put("grant_type", "authorization_code");
		paraMap.put("sign_type", "RSA");
		paraMap.put("timestamp", DateUtils.format(new Date(), DateUtils.DATE_TIME_PATTERN));
		paraMap.put("method", "alipay.system.oauth.token");*/
		//MapUtils.formatUrlMap(paraMap, false, false);
		AlipayUserUserinfoShareResponse userinfoShareResponse = new AlipayUserUserinfoShareResponse();
		try {
			AlipayClient alipayClient =new DefaultAlipayClient(oauth2Url,appID,key,"json","GBK",publicKey);
			AlipaySystemOauthTokenRequest request = new AlipaySystemOauthTokenRequest();
			request.setCode(code);
			request.setGrantType("authorization_code");
			AlipaySystemOauthTokenResponse oauthTokenResponseUser = alipayClient.execute(request);
			System.out.println("******:"+oauthTokenResponseUser.getAccessToken());
			AlipayClient alipayClientUser =new DefaultAlipayClient(oauth2Url,appID,key,"json","GBK",publicKey);
			AlipayUserUserinfoShareRequest requestUser = new AlipayUserUserinfoShareRequest();
			userinfoShareResponse = alipayClientUser.execute(requestUser, oauthTokenResponseUser.getAccessToken());
			System.out.println("**************:"+userinfoShareResponse.getBody());
			System.out.println("用户支付宝ID:" + userinfoShareResponse.getUserId());//用户支付宝ID
			System.out.println("用户支付宝ID:" + userinfoShareResponse.getAlipayUserId());//用户支付宝ID
			System.out.println("用户头像:" + userinfoShareResponse.getAvatar());//用户头像
			System.out.println("用户昵称:" + userinfoShareResponse.getNickName());//用户昵称
			//System.out.println("用户类型:" + userinfoShareResponse.getUserType());//用户类型
			System.out.println("用户状态:" + userinfoShareResponse.getUserStatus());//用户状态
			System.out.println("Email地址:" + userinfoShareResponse.getEmail());//用户Email地址
			System.out.println("用户是否进行身份认证:" + userinfoShareResponse.getIsCertified());//用户是否进行身份认证
			System.out.println("用户是否进行学生认证:" + userinfoShareResponse.getIsStudentCertified());//用户是否进行学生认证
		/*	AlipayClient client = new DefaultAlipayClient(oauth2Url, appID, key,"json","GBK",publicKey,"RSA");
			AlipaySystemOauthTokenRequest oauthTokenRequest = new AlipaySystemOauthTokenRequest();
            oauthTokenRequest.setCode(code);
            oauthTokenRequest.setGrantType("authorization_code");
            AlipaySystemOauthTokenResponse oauthTokenResponse = client.execute(oauthTokenRequest);
            userid= oauthTokenResponse.getUserId();
			System.out.println("UserId----: "+oauthTokenResponse.getUserId()+"  ====AccessToken:"+oauthTokenResponse.getAccessToken()+"  ===alipayUserId:"+oauthTokenResponse.getAlipayUserId());
*/
		} catch (Exception e) {
			e.printStackTrace();
		}
		//String result = HttpUtils.requestHttp(oauth2Url, "post", params);
		//String result = userid;
		//JSONObject json = HttpClientUtils.httpPost(oauth2Url, params);
		//System.out.println("result------" + json.toJSONString());
		return userinfoShareResponse;
	}
}

